package com.dx.mes.common.dao.impl;

import com.dx.common.table.ppt.Spc_ret_chart_raw;
import com.dx.common.table.ppt.Spc_ret_chart_rawId;
import com.dx.mes.common.core.dao.impl.GenericHibernateDao;
import com.dx.mes.common.dao.IChartRawDao;
import com.dx.mes.common.table.rtnBean.spc.ChartRawDataKey;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @Title: ChartRawDaoImpl.java
 * @Package com.daoimpl
 * @Description: TODO(添加描述)
 * @author Lynn.Sea
 * @date 2014-2-20 下午2:16:59
 * @version V1.0
 */
@Repository("chartRawDao")
public class ChartRawDaoImpl extends GenericHibernateDao<Spc_ret_chart_raw, Spc_ret_chart_rawId>
implements IChartRawDao {
    @Resource
    private SessionFactory sessionFactory;

    @Resource
    private IChartRawDao chartRawDao;

    @Override
    public Spc_ret_chart_raw getChartRawData(Spc_ret_chart_raw spc_ret_chart_raw) {
        Session session = sessionFactory.getCurrentSession();
        Criteria criteria = session.createCriteria(Spc_ret_chart_raw.class);
        criteria.add(Example.create(spc_ret_chart_raw));
        /*
         * Criteria criteria = session.createCriteria(Spc_ret_chart_raw.class);
         * criteria.add(Restrictions.eq("col_typ_fk", inPo.getCol_typ()));
         * criteria.add(Restrictions.eq("grp_no_fk", inPo.getGrp_no()));
         * criteria.add(Restrictions.eq("chart_no_fk", inPo.getChart_no()));
         * criteria.add(Restrictions.eq("proc_time_d", inPo.getInput_time_d()));
         * criteria.add(Restrictions.eq("prd_value_seq",
         * inPo.getPrd_value_seq()));
         */
        criteria.addOrder(Order.asc("prd_value_seq"));
        return (Spc_ret_chart_raw) criteria.uniqueResult();
    }

    /**
     * TODO:此处代完善，查询rawData暂时只提供一种方法 因为double类型的数据查询时不能用=只能用like 所以不能用QBE
     * Example查询
     */
    public List<Spc_ret_chart_raw> listChartRawData(Spc_ret_chart_raw spc_ret_chart_raw, String sub_grp_no) {
        /*
         * List<Spc_ret_chart_raw> rawList =
         * genericDaoImpl.listRecordersByExample(spc_ret_chart_raw); return
         * rawList;
         */
        Session session = sessionFactory.getCurrentSession();
        Criteria criteria = session.createCriteria(Spc_ret_chart_raw.class);
        criteria.add(Restrictions.eq("col_typ_fk", spc_ret_chart_raw.getId().getCol_typ_fk()));
        criteria.add(Restrictions.eq("grp_no_fk", sub_grp_no));
        criteria.add(Restrictions.eq("chart_no_fk", spc_ret_chart_raw.getId().getChart_no_fk()));
        criteria.add(Restrictions.like("proc_time_d", spc_ret_chart_raw.getId().getProc_time_d()));
        return criteria.list();
    }

    @Override
    public Map<ChartRawDataKey, List<Spc_ret_chart_raw>> listChartRawData(String colTypFk, String grpNoFk,
                                                                          String chartNoFk, List<Double> procTimeDList) {
        /*
         * List<Spc_ret_chart_raw> rawList =
         * genericDaoImpl.listRecordersByExample(spc_ret_chart_raw); return
         * rawList;
         */
        StringBuffer hql = new StringBuffer(
                "FROM Spc_ret_chart_raw where col_typ_fk = ? and grp_no_fk = ? and chart_no_fk  = ? ");
        hql.append(" and  proc_time_d >= ?");
        Session session = sessionFactory.getCurrentSession();
        Query query = session.createQuery(hql.toString());
        query.setString(0, colTypFk);
        query.setString(1, grpNoFk);
        query.setString(2, chartNoFk);
        query.setDouble(3, getMin(procTimeDList));
        List<Spc_ret_chart_raw> rawDataList = query.list();
        Map<ChartRawDataKey, List<Spc_ret_chart_raw>> rawDataMap = new HashMap<ChartRawDataKey, List<Spc_ret_chart_raw>>();
        for (Spc_ret_chart_raw rawData : rawDataList) {
            ChartRawDataKey rawDataKey = new ChartRawDataKey(rawData.getId().getCol_typ_fk(), rawData.getId().getGrp_no_fk(),
                    rawData.getId().getChart_no_fk(), rawData.getId().getProc_time_d());
            List<Spc_ret_chart_raw> chartRawList = rawDataMap.get(rawDataKey);
            if (chartRawList == null) {
                chartRawList = new ArrayList<Spc_ret_chart_raw>();
            }
            chartRawList.add(rawData);
            rawDataMap.put(rawDataKey, chartRawList);
        }

        return rawDataMap;
    }

    private Double getMin(List<Double> dList) {

        if (dList.isEmpty()) {
            return null;
        }
        Double min = dList.get(0);
        for (Double d : dList) {
            if (d < min) {
                min = d;
            }
        }
        return min;
    }
}
